package com.adcc.loadsheet.monitor.entity;

import com.adcc.loadsheet.monitor.util.AlarmEnum;
import com.adcc.loadsheet.monitor.util.Constant;
import com.adcc.loadsheet.monitor.util.StringUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * AppUpload监控信息实体
 */
public class AppUploadMonitor extends BaseEntity implements Cloneable {

    // 远程时间（运行程序的本地时间）
    private String remoteDateTime;

    // 本地时间（本地进行NTP授时，即NTP时间）
    private String localDateTime;

    // 时间分钟差
    private int minutesBetween;

    // NTP状态
    private boolean ntpStatus;

    // 舱单接收总数
    private long receiveCount;

    // 舱单拆分失败数
    private long failCount;

    // 最新报文接收时间
    private String lastRecvTime;

    // 舱单拆分失败率
    private double failureRate;

    // 舱单拆分服务状态
    private boolean loadsheetStatus;

    // 服务状态
    private MonitorStatus status = MonitorStatus.ABNORMAL;

    // 异常状态码(0：正常，1：异常，状态机:00,例如11标识模块监控项均异常)
    private String errorCode = "00";

    // 异常原因：XXX IS DOWN,XXX IS DOWN
    private String errorReason = "";

    // 本模块异常的节点
    private Set<String> alarmContent = new HashSet<>();

    // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
    private Set<String> unAlarmContent = new HashSet<>();

    public String getRemoteDateTime() {
        return remoteDateTime;
    }

    public void setRemoteDateTime(String remoteDateTime) {
        this.remoteDateTime = remoteDateTime;
    }

    public String getLocalDateTime() {
        return localDateTime;
    }

    public void setLocalDateTime(String localDateTime) {
        this.localDateTime = localDateTime;
    }

    public int getMinutesBetween() {
        return minutesBetween;
    }

    public void setMinutesBetween(int minutesBetween) {
        this.minutesBetween = minutesBetween;
    }

    public boolean isNtpStatus() {
        return ntpStatus;
    }

    public void setNtpStatus(boolean ntpStatus) {
        this.ntpStatus = ntpStatus;
    }

    public long getReceiveCount() {
        return receiveCount;
    }

    public void setReceiveCount(long receiveCount) {
        this.receiveCount = receiveCount;
    }

    public long getFailCount() {
        return failCount;
    }

    public void setFailCount(long failCount) {
        this.failCount = failCount;
    }

    public String getLastRecvTime() {
        return lastRecvTime;
    }

    public void setLastRecvTime(String lastRecvTime) {
        this.lastRecvTime = lastRecvTime;
    }

    public double getFailureRate() {
        return failureRate;
    }

    public void setFailureRate(double failureRate) {
        this.failureRate = failureRate;
    }

    public boolean isLoadsheetStatus() {
        return loadsheetStatus;
    }

    public void setLoadsheetStatus(boolean loadsheetStatus) {
        this.loadsheetStatus = loadsheetStatus;
    }

    public MonitorStatus getStatus() {
        return status;
    }

    public void setStatus(MonitorStatus status) {
        this.status = status;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorReason() {
        return errorReason;
    }

    public void setErrorReason(String errorReason) {
        this.errorReason = errorReason;
    }

    public Set<String> getAlarmContent() {
        return alarmContent;
    }

    public void setAlarmContent(Set<String> alarmContent) {
        this.alarmContent = alarmContent;
    }

    public Set<String> getUnAlarmContent() {
        return unAlarmContent;
    }

    public void setUnAlarmContent(Set<String> unAlarmContent) {
        this.unAlarmContent = unAlarmContent;
    }

    @Override
    public String toXML() throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws Exception {
        return null;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        AppUploadMonitor appUpload = new AppUploadMonitor();
        appUpload.setRemoteDateTime(remoteDateTime);
        appUpload.setLocalDateTime(localDateTime);
        appUpload.setMinutesBetween(minutesBetween);
        appUpload.setNtpStatus(ntpStatus);
        appUpload.setReceiveCount(receiveCount);
        appUpload.setFailCount(failCount);
        appUpload.setLastRecvTime(lastRecvTime);
        appUpload.setFailureRate(failureRate);
        appUpload.setLoadsheetStatus(loadsheetStatus);
        appUpload.setStatus(status);
        appUpload.setErrorCode(errorCode);
        appUpload.setErrorReason(errorReason);
        appUpload.setAlarmContent(alarmContent);
        appUpload.setUnAlarmContent(unAlarmContent);
        return appUpload;
    }

    /**
     * 异常状态码
     */
    public void buildErrorCode() {
        StringBuilder sb = new StringBuilder();
        char[] states = new char[2];
        states[0] = ntpStatus ? '0' : '1';
        states[1] = loadsheetStatus ? '0' : '1';
        for(char c : states){
            sb.append(c);
        }
        this.errorCode = sb.toString();

        // [2024-6-01：张丽娟：增加声音和弹窗告警的功能，系统支持对指定告警项进行声音和弹窗告警忽略的功能: 根据状态码获取正常节点和异常节点信息]
        // 本模块异常的节点
        Set<String> alarmContent = new HashSet<>();
        // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
        Set<String> unAlarmContent = new HashSet<>();
        // 保存到数据库的异常信息
        StringBuilder errorReasonBuilder = new StringBuilder();

        AlarmEnum enumNtp = AlarmEnum.APPUPLOAD_NTP;
        if (states[0] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumNtp.getMysqlLogName()));
            alarmContent.add(enumNtp.getItem());
        }else{
            unAlarmContent.add(enumNtp.getItem());
        }

        AlarmEnum enumLoadSheet = AlarmEnum.APPUPLOAD_LOADSHEET;
        if (states[1] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumLoadSheet.getMysqlLogName()));
            alarmContent.add(enumLoadSheet.getItem());
        }else{
            unAlarmContent.add(enumLoadSheet.getItem());
        }

        this.alarmContent = alarmContent;
        this.unAlarmContent = unAlarmContent;
        this.errorReason = StringUtil.stringBuilderToString(errorReasonBuilder);
    }
}
